package ru.spb.osll.myfit.common.filter;

import org.junit.Before;
import org.junit.Test;
import static junit.framework.Assert.*;
import static org.easymock.EasyMock.*;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


/**
 * User: Alexander Opeykin
 * Date: 5/1/12
 */
public class FilterSerializerTest {
  FilterSerializer m_serializer;

  @Before
  public void prepare() {
    m_serializer = new FilterSerializer();
  }

  @Test
  public void testEmptyDeserialization() throws Exception {
    assertEquals(0, m_serializer.deserialize("").size());
  }

  @Test
  public void testNoEqualsSignDeserialization() throws Exception {
    assertEquals(0, m_serializer.deserialize("SomeTextHere").size());
  }

  @Test
  public void testEmptyValueDeserialization() throws Exception {
    assertEquals(1, m_serializer.deserialize("key=").size());
  }

  @Test
  public void testEmptyKeyDeserialization() throws Exception {
    assertEquals(0, m_serializer.deserialize("=value").size());
  }

  @Test
  public void testDeserializationWithPipe() throws Exception {
    assertEquals(1, m_serializer.deserialize("key=value|").size());
  }

  @Test
  public void testDeserializationWithoutPipe() throws Exception {
    assertEquals(1, m_serializer.deserialize("key=value").size());
  }

  @Test
  public void testDeserializatexpect() throws Exception {
    List<PageResultFilter> filters = Arrays.asList(new PageResultFilter[]{
            new MatchFilter("key1", "value1"),
            new MatchFilter("key2", "value2"),
            new MatchFilter("key3", "value3"),
    });

    String string = "key1=value1|key2=value2|key3=value3|";

    List<PageResultFilter> result = m_serializer.deserialize(string);
    assertEquals(filters, result);
  }

  @Test
  public void testSerialization() throws Exception {
    PageResultFilter mFilter1 = createNiceMock(PageResultFilter.class);
    expect(mFilter1.serialize()).andReturn("mock1").once();
    replay(mFilter1);

    PageResultFilter mFilter2 = createNiceMock(PageResultFilter.class);
    expect(mFilter2.serialize()).andReturn("mock2").once();
    replay(mFilter2);

    String expectedString = "mock1|mock2|";
    List<PageResultFilter> mFilters = Arrays.asList(mFilter1, mFilter2);

    assertEquals(expectedString, m_serializer.serialize(mFilters));
    verify(mFilter1);
    verify(mFilter2);
  }
  
  @Test
  public void testRealLife(){
	  List<PageResultFilter> expectedFilters = new LinkedList<PageResultFilter>();
	  expectedFilters.add(new MatchFilter("1","one"));
	  expectedFilters.add(new MatchFilter("2","two"));
	  expectedFilters.add(new MatchFilter("3","three"));
	  expectedFilters.add(new MatchFilter("4","four"));
	  expectedFilters.add(new MatchFilter("0",""));
	  
	  FilterSerializer serializer = new FilterSerializer();
	  String realFilterString = serializer.serialize(expectedFilters);
	  
	  List<PageResultFilter> realFilters = serializer.deserialize(realFilterString);
	  assertEquals(expectedFilters.size(), realFilters.size());
  }
}
